﻿Ext.extend(demo.module,{
    init: function(){  
 	  var opt={ 
	  url:'galleryquery'	  
	  };
	  var pageParams={start:0,limit:10};	
	  var mask= new Ext.LoadMask(Ext.getBody(), {msg:"Please wait..."});
	  opt.fields=[
	  {name:'id',text:'id',header:{hidden:true},ft:'hidden'},	 
	  {name:'image',text:'图片',header:{width:220,renderer:function(v,m,r){
	    return "<img src='"+ctx_path+"/image/"+v+".jpg"+"' width='400px;'/>"	    
	  }}},	
	  {name:'des',text:'描述'},
	  {name:'seq',text:'权重'}
	  ];
	  opt.formConfig={	    
	     bodyStyle:'padding:5px 2px 0 5px',  
	     autoWidth:true	  
	  };	 
	  opt.formConfig.items=[
	   {	     
	     title:'图片(600x400)',
	     columnWidth:.1,
	     autoHeight:true,
	     layout:'fit',	    
	     xtype:'fieldset',
	     bodyStyle:'padding:3px',  
	     items:[
	       {xtype:'imagefield',uploadUrl:ctx_path+'/gup',swfuploadBase:ctx_path+"/upload",id:'logimage',
	         listeners:{
	          imageload:function()
	          {
	            var win=g.window;   	         
	            win.doLayout();	            
	            win.getEl().center();	       
	          },
	          imageupload:function(data)
	          {
	             if(data.ok)
	             {	               
	               this.loadImage(ctx_path+"/image/"+data.id+".jpg");
	               g.form.getForm().findField("image").setValue(data.id);
	             }   
	             else
	             {
	               alert("上传失败！")
	             } 
	          }
	         }
	       }
	     ]
	   },
	   {
	     title:'描述',
	     layout:'form',
	     columnWidth:.1,	
	     xtype:'fieldset',
	     autoHeight:true,
	     defaultType:'textfield',
	     labelAlign:'right',
	     defaults:{anchor:'93%'},
	     items:[	    
	      {name:'id',xtype:'hidden'},
	      {name:'image',xtype:'hidden'},    	
		  {name:'des',fieldLabel:'文字描述',allowBlank:false},	 
		  {name:'seq',fieldLabel:'权重',xtype:'numberfield',allowBlank:false,allowNegative:false,allowDecimals:false}	
	     ]	     
	   }
	  ];
	  opt.windowConfig={width:500};	  
	  opt.gridConfig={};	
	  opt.gridConfig.tbar=[
	   {text:'添加',iconCls:'add',handler:function(x){
	     var load=false;
	     if(this.window.el)
	     {
	       load=true;
	     }
	     this.window.setTitle(x);
	     this.window.show();
	     this.window.center();
	     if(load){Ext.getCmp("logimage").loadDefaultImage();}
	     
	   }},
	   {
	     text:'修改',iconCls:'edit',handler:function(x)
	     {
	       this.window.setTitle(x);
	       this.edit();	 
	       this.window.center();
	       var id=this.form.getForm().findField("image").getValue();	      
	       Ext.getCmp("logimage").loadImage(ctx_path+"/image/"+id+".jpg");	    
	     }
	   },
	   {
	     text:'删除',iconCls:'delete',handler:function(x)
	     {	       
	       this.del(function(rs){
	         this.postJson({
	           url:'deletegallery',
	           params:{gallery:{id:rs[0].get("id")}}
	         })
	       })
	     }	   
	   }    
	  ];       
     opt.gridConfig.bbar={pageSize:pageParams.limit};     
	 var g=new Ext.fay.ui.CURDGrid(opt);
	 g.on("save",function(values){
	    if(this.isValid())
	    {
	      this.postJson({
	       url:'savegallery',
	       params:{gallery:values}
	      })
	    }
	 });  
	 this.main.add(g.grid);
     this.main.doLayout(); 
     g.ds.reload({params:pageParams});
   }
});